import { toast, checkPhone } from "../../../utils/util";
const app = getApp()

// personal/person/info/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    userIsShow: false,
    phoneIsShow: false,
    headurl: '',
    nickname: '',
    entname: '',
    username: '',
    phone: '',
    registdate: '',
    ideisauth: 0,
    entisauth: 0,
    changePhone: '',
    codeTime: '获取验证码',
    time: null,
    code: '',
    showCode: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initInfo()
  },
  initInfo() {
    const {
      headurl,
      nickname,
      entname,
      username,
      phone,
      registdate,
      ideisauth,
      entisauth
    } = wx.getStorageSync('userInfo')
    this.setData({
      headurl,
      nickname,
      entname,
      username,
      phone,
      registdate,
      ideisauth,
      entisauth
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindChangeName() {
    this.setData({
      show: true,
      userIsShow: true
    })
  },
  onClose() {
    this.setData({
      show: false,
      userIsShow: false,
      phoneIsShow: false
    })
  },
  bindChangePhone() {
    this.setData({
      show: true,
      phoneIsShow: true
    })
  },
  bindCompanyAuth() {
    wx.navigateTo({
      url: '/vip/enterprise/index'
    })
  },
  bindUserAuth() {
    wx.navigateTo({
      url: '/vip/identity/index'
    })
  },
  bindCommit() {
    const reg = /^1[3456789]\d{9}/
    const {changePhone,phone,code} = this.data
    if(!reg.test(changePhone)){
      toast('手机格式不正确', 1000)
      return
    }
    if(phone.trim() === changePhone.trim()) {
      toast('手机号码一样，可不修改', 1000)
      return
    }
    if(code.trim() === '') {
      toast('验证码不能为空', 1000)
      return
    }
    app.$api.user.changePhone({
      phone: changePhone,
      code
    })
      .then((res) => {
        wx.showToast({
          title: '修改成功',
          icon: 'success',
          duration: 1000
        })
      }).catch((e) => {
        toast('请稍后再试', 1000)
      })
  },
  bindPhone(e) {
    const { value } = e.detail
    this.setData({
      changePhone: value
    })
  },
  bindGetCode() {
    const phone = this.data.changePhone
    if (!checkPhone(phone)) {
      wx.showToast({
        title: '手机号码格式不正确',
        icon: 'none',
        duration: 1000
      })
      return false
    }
    this.checkTime()
  },
  checkTime() {
    const GET_CODE = '获取验证码'
    const date = +new Date()
    const minute = 1 * 60 * 1000
    const beforeTime = wx.getStorageSync('GET_CODE_TIME_TWO_TWO')
    const reduceTime = Math.ceil(60 - (date - beforeTime) / 1000)
    console.log(beforeTime)
    if (beforeTime) {
      if (date - Number(beforeTime) > minute) {
        wx.setStorageSync('GET_CODE_TIME_TWO', date)
        this.setData({
          codeTime: 60
        })
        this.countDown()
        this.getCode()
      } else {
        wx.showToast({
          title: `请${reduceTime}秒后再试`,
          icon: 'none',
          duration: 1000
        })
      }
    } else {
      this.setData({
        codeTime: 60
      })
      wx.setStorageSync('GET_CODE_TIME_TWO', date)
      this.countDown()
      this.getCode()
    }
  },
  countDown() {
    let codeTime = this.data.codeTime
    if (this.data.timer) clearTimeout(this.data.timer)
    if (codeTime > 0) {
      this.setData({
        codeTime: --codeTime
      })
      this.data.timer = setTimeout(() => {
        this.countDown()
      }, 1000)
    } else {
      this.setData({
        codeTime: '获取验证码'
      })
    }
  },
  getCode() {
    const phone = this.data.changePhone
    app.$api.login.getSendMsg({
      phone
    }).then((res) => {
      res = res.data
      if (res.success) {
        return
      }
    }).catch((e) => {
      toast('请稍后再试', 1000)
    })
  },
  bindCode(e) {
    this.setData({
      code: e.detail.value
    })
  },
  bindPopularize() {
    this.setData({
      showCode: true
    })
  },
  onClose() {
    this.setData({
      showCode: false
    })
  }
})